//
//  HSYTableViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/1.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYBaseTableViewController.h"
#import <Masonry.h>
#import "MJRefresh.h"
#import "GzwTableViewLoading.h"
#import "UITableView+HSYBase.h"

@interface HSYBaseTableViewController ()

@end

@implementation HSYBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageIdex = 0;
    self.pageStep = 10;
    
    if ([self hsy_showEmptyImage]) {
        [self setupEmptyImage];
    }
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo([self hsy_tableViewEdgeInsets]);
    }];
    
    [self addRefreshFooterIfNeed];
    [self addRefreshHeaderIfNeed];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setupEmptyImage{
    [self.tableView setupMessage:[self hsy_emptyTitle] imageName:[self hsy_emptyImageName]];
    self.tableView.gzw_contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    
}

#pragma mark - 加载tableview -

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

#pragma mark - Private Methods -
- (void)addRefreshFooterIfNeed{
    if ([self hsy_allowPullUp]) {
        __weak typeof(self) wSelf = self;
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong typeof(wSelf) strongSelf = wSelf;
            [strongSelf hsy_handlePullUpToLoadMore];
        }];
        
        MJRefreshAutoNormalFooter *footer = (id)self.tableView.mj_footer;
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"加载更多" forState:MJRefreshStateRefreshing];
        [footer setTitle:@"已加载全部数据" forState:MJRefreshStateNoMoreData];
        footer.stateLabel.textColor = [UIColor hsyLoadMoreBtnColor];
        self.tableView.mj_footer.automaticallyHidden = YES;
    }
}

- (void)addRefreshHeaderIfNeed{
    if ([self hsy_allowPullDown]) {
        __weak typeof(self) wSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong typeof(wSelf) strongSelf = wSelf;
            [strongSelf hsy_handlePullDownToLoadNew];
        }];
    }
}

#pragma mark - Public Methods -

- (BOOL)hsy_showEmptyImage{
    return NO;
}
- (NSString *)hsy_emptyImageName{
    return @"empty";
}
- (NSString *)hsy_emptyTitle{
    return @"对不起，暂无结果";
}

- (UIEdgeInsets)hsy_tableViewEdgeInsets{
    return UIEdgeInsetsZero;
}

- (BOOL)hsy_allowPullUp{
    return YES;
}

- (BOOL)hsy_allowPullDown{
    return YES;
}

- (void)hsy_headerBeginRefreshing{
    [self.tableView.mj_header beginRefreshing];
}

- (void)hsy_headerEndRefreshing{
    [self.tableView.mj_header endRefreshing];
}

- (void)hsy_footerBeginRefreshing{
    [self.tableView.mj_footer beginRefreshing];
}

- (void)hsy_footerEndRefreshing{
    [self.tableView.mj_footer endRefreshing];
}

- (void)hsy_footerEndRefreshingWithNoMoreData{
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)hsy_handlePullDownToLoadNew{
    self.pageIdex = 0;
    [self hsy_loadServerData];
}

- (void)hsy_handlePullUpToLoadMore{
    self.pageIdex ++;
    [self hsy_loadServerData];
}

-(void)hsy_loadServerDataComplete:(HSYDataLoadCompleteStatus) status{
    if (self.tableView.mj_header.state == MJRefreshStateRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    
    if (self.tableView.mj_footer.state == MJRefreshStateRefreshing) {
        if (status == kDataLoadCompleteStatusNoMoreData) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
    }
    [self.tableView reloadData];
    
    if (status == kDataLoadCompleteStatusFailed) {
        [self loadServerDataNetworkFaild];
    }
    if ([self hsy_showEmptyImage]) {
        self.tableView.loading = NO;
    }


}
- (void)hsy_loadServerData{
    if ([self hsy_showEmptyImage]) {
        self.tableView.loading = YES;
    }

}

- (void)loadServerDataNetworkFaild{
    self.pageIdex -= 1;
}

#pragma mark - UITableViewDelegate & UITableViewDataSource -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
